<% layout('/layouts/default.html', {title: '物料转换单管理', libs: ['validate','dataGrid']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header with-border">
			<div class="box-title">
				<i class="fa icon-note"></i> ${text(stMaterialConvertBill.isNewRecord ? '新增物料转换单' : '编辑物料转换单')}
			</div>
			<div class="box-tools pull-right">

				<a href="#" class="btn btn-default" id="btn-choose" ><i class="fa fa-filter"></i> ${text('选择物料')}
				</a>
				<a href="#" class="btn btn-default" id="btn-approve" ><i class="fa fa-filter"></i> ${text('审核')}
				</a>
				<a href="#" class="btn btn-default" id="btn-revocation" ><i class="fa fa-filter"></i> ${text('弃审')}
				</a>
				<a href="#" class="btn btn-default" id="btn-save" ><i class="fa fa-filter"></i> ${text('保存')}
				</a>
				<div class="hide">
					<#form:listselect id="btn-src" title="仓库详情"

					url="${ctx}/st/stWarehouseInventory/listAll" allowClear="false"
					checkbox="false"  callbackFuncName="listselectCallback"/>
				</div>
				<script>
					$("#btn-choose").click(function () {
						$("#btn-srcButton").click();
					});
					var aStatus=${stMaterialConvertBill.approveStatus!"null"};
					approveBtnStatus(aStatus);
					function approveBtnStatus(status) {
						var $save = $("#btn-save");
						var $approve = $("#btn-approve");
						var $revocation = $("#btn-revocation");
						if (null == status ) {
							$save.show();
							$approve.hide();
							$revocation.hide();
						}
						if (2 == status) {
							$save.show();
							$approve.show();
							$revocation.hide();
						}
						if (4 == status) {
							$save.hide();
							$approve.hide();
							$revocation.show();
						}
					}
					$("#btn-approve").click(function () {
						$("#approveStatus").val(4);
						$("form").submit();
					});
					$("#btn-save").click(function () {
						$("#approveStatus").val(2);
						$("form").submit();
					});
					$("#btn-revocation").click(function () {
						$("#approveStatus").val(2);
						$("form").submit();
					});
					function refresh(id) {
						location.replace("${ctx}/st/stMaterialConvertBill/form?id="+id);
						// location.reload();
					}
					var addLength = ${stMaterialConvertBill.stMaterialConvertBillDetailList.~size!"0"};
					function listselectCallback(id, act, index0, layero, selectData){
						if(!selectData) return;
						console.info(selectData);
						for (var i = 0; i < selectData.length; i++) {
							$("#stMaterialConvertBillDetailDataGridAddRowBtn").click();
						}
						for (var index in selectData) {
							var material = $(($(($("#stMaterialConvertBillDetailDataGrid").find("tr"))[parseInt(index) + 1 + addLength]).find('td'))[3]);//前面3行都什么啊
							var item = selectData[index];
							var find = material.find(".form-control");
							find.val(item.materialCode).trigger('change');
							material.find("input:hidden").val(item.materialCode);

							var batchNumber = material.next();
							batchNumber.find(".form-control").val(item.batchNumber);

							var barCode = batchNumber.next();
							barCode.find(".form-control").val(item.materialCode + "_" + item.batchNumber);

							var number = barCode.next();
							number.find(".form-control").val(item.number);

							var outWarehouse = number.next();
							outWarehouse.find(".form-control").val(item.warehouseId).trigger('change');
							outWarehouse.find("input:hidden").val(item.warehouseId);

							var productDate = outWarehouse.next().next().next().next().next();
							productDate.find(".form-control").val(item.productDate);
							var savedType = productDate.next();
							savedType.find(".form-control").val(item.savedType);
							var passDate = savedType.next();
							passDate.find(".form-control").val(item.passDate);

							addLength++;
						}
					}
					$(function () {
						$("#stMaterialConvertBillDetailDataGrid").on("select2:select",".convertMaterialCode",function (e) {
							var data = e.params.data;
							if(""== data.id) return;
							// console.info(data.id);
							// 更改后条码
						});
					});
				</script>
				<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
			</div>
		</div>
		<#form:form id="inputForm" model="${stMaterialConvertBill}" action="${ctx}/st/stMaterialConvertBill/save" method="post" class="form-horizontal">
			<div class="box-body">
				<div class="form-unit">${text('基本信息')}</div>
				<#form:hidden path="id"/>
				<div class="row">
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('单据号')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:input path="billNo" maxlength="20" class="form-control"/>-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('单据日期')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="billDate" readonly="true" maxlength="20" class="form-control laydate"
									dataFormat="datetime" data-type="datetime" data-format="yyyy-MM-dd HH:mm"/>
							</div>
						</div>
					</div>
					<#form:hidden path="approveStatus" />
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('审批状态')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:select path="approveStatus" dictType="st_approve_state" blankOption="true" class="form-control digits" />-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('审批人')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:input path="approveMan" maxlength="5" class="form-control"/>-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('审批人编码')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:input path="approveManCode" maxlength="20" class="form-control"/>-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('申请人')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:treeselect id="applyMan" title="用户选择" path="applyManCode" labelPath="applyMan"
								url="${ctx}/sys/office/treeData?isLoadUser=true" class="" allowClear="true"/>
							</div>
						</div>
					</div>
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('申请人编码')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:input path="applyManCode" maxlength="20" class="form-control"/>-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
				</div>
				<div class="row">
					<div class="col-xs-12">
						<div class="form-group">
							<label class="control-label col-sm-2" title="">
								<span class="required hide">*</span> ${text('备注')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-10">
								<#form:textarea path="remarks" rows="4" maxlength="500" class="form-control"/>
							</div>
						</div>
					</div>
				</div>
				<h4 class="form-unit">${text('物料转换单详情')}</h4>
				<div class="ml10 mr10">
					<table id="stMaterialConvertBillDetailDataGrid"></table>
					<% if (hasPermi('st:stMaterialConvertBill:edit')){ %>
						<a href="#" id="stMaterialConvertBillDetailDataGridAddRowBtn" style="display: none" class="btn btn-primary btn-sm mt10 mb10"><i class="fa fa-plus"></i> ${text('增行')}</a>
					<% } %>
				</div>
			</div>
			<div class="box-footer">
				<div class="row">
					<div class="col-sm-offset-2 col-sm-10">
<!--						<% if (hasPermi('st:stMaterialConvertBill:edit')){ %>-->
<!--							<button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i> ${text('保 存')}</button>&nbsp;-->
<!--						<% } %>-->
						<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
					</div>
				</div>
			</div>
		</#form:form>
	</div>
</div>
<% } %>
<script>
//初始化物料转换单详情DataGrid对象
$("#stMaterialConvertBillDetailDataGrid").dataGrid({

	data: ${toJson(stMaterialConvertBill.stMaterialConvertBillDetailList)},
	datatype: "local", // 设置本地数据
	autoGridHeight: function(){return 'auto'}, // 设置自动高度

	// 设置数据表格列
	columnModel: [
		{header:'状态', name:'status', editable:true, hidden:true},
		{header:'主键', name:'id', editable:true, hidden:true},
		{header:'${text("物料名称")}', name:'materialCode', width:150,
			editable:true, edittype:'select', editoptions:{'class':'form-control',
				items: $.merge([{materialName:'&nbsp;',materialCode:''}], ${toJson(mpList)}),
				itemLabel: 'materialName', itemValue: 'materialCode', dataInit: function(element){
					$(element).select2().on("change",function(){$(this).resetValid()});
					element.name="ignore_materialCode";
					var name=element.name;
					$(element).attr("disabled",true);
					$(element).after("<input name='materialCode' value='"+element.value+"' hidden>");
				}
			}
		},
		{header:'${text("批号")}', name:'batchNumber', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'20', 'class':'form-control',readonly: true}},
		{header:'${text("条码号")}', name:'barCode', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'30', 'class':'form-control',readonly: true}},
		{header:'${text("数量")}', name:'number', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'10', 'class':'form-control digits'}},
		{header:'${text("调出仓位")}', name:'outWarehouse', width:150,
			editable:true, edittype:'select', editoptions:{'class':'form-control',
				items: $.merge([{warehouseName:'&nbsp;',id:''}], ${toJson(warehouseList)}),
				itemLabel: 'warehouseName', itemValue: 'id', dataInit: function(element){
					$(element).select2().on("change",function(){$(this).resetValid()});
					element.name="ignore_outWarehouse";
					var name=element.name;
					$(element).attr("disabled",true);
					$(element).after("<input name='outWarehouse' value='"+element.value+"' hidden>");
				}
			}
		},
		{header:'${text("调入仓位")}', name:'inWarehouse', width:150,
			editable:true, edittype:'select', editoptions:{'class':'form-control',
				items: $.merge([{warehouseName:'&nbsp;',id:''}], ${toJson(warehouseList)}),
				itemLabel: 'warehouseName', itemValue: 'id', dataInit: function(element){
					$(element).select2().on("change",function(){$(this).resetValid()});
					// element.name="ignore_inWarehouse";
					// var name=element.name;
					// $(element).attr("disabled",true);
					// $(element).after("<input name='inWarehouse' value='"+element.value+"' hidden>");
				}
			}
		},
		{header:'${text("转换后物料名称")}', name:'convertMaterialCode', width:150,
			editable:true, edittype:'select', editoptions:{'class':'form-control convertMaterialCode',
				items: $.merge([{materialName:'&nbsp;',materialCode:''}], ${toJson(mpList)}),
				itemLabel: 'materialName', itemValue: 'materialCode', dataInit: function(element){
					$(element).select2().on("change",function(){$(this).resetValid()});
					// element.name="ignore_convertMaterialCode";
					// var name=element.name;
					// $(element).attr("disabled",true);
					// $(element).after("<input name='convertMaterialCode' value='"+element.value+"' hidden>");
				}
			}
		},
		{header:'${text("转换后条码号")}', name:'converrBarCode', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'30', 'class':'form-control'}},
		{header:'${text("包装规格")}', name:'packType', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'4', 'class':'form-control'}},
		{header:'${text("生产日期")}', name:'productDate', width:150,
			formatter:'date', formatoptions:{srcformat:'Y-m-d H:i:s',newformat:'Y-m-d H:i:s'},
			editable:true, edittype:'text', editoptions:{'class':'form-control laydate', 'readonly':'true',
				dataInit: function(element){
					// laydate.render({elem:element, type:'datetime', format:'yyyy-MM-dd HH:mm'});
				}
			}
		},
		{header:'${text("保质期")}', name:'savedType', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'10', 'class':'form-control digits'}},
		{header:'${text("到期日期")}', name:'passDate', width:150,
			formatter:'date', formatoptions:{srcformat:'Y-m-d H:i:s',newformat:'Y-m-d H:i:s'},
			editable:true, edittype:'text', editoptions:{'class':'form-control laydate', 'readonly':'true',
				dataInit: function(element){
					// laydate.render({elem:element, type:'datetime', format:'yyyy-MM-dd HH:mm'});
				}
			}
		},
		{header:'${text("备注")}', name:'remarks', width:150, editable:true, edittype:'textarea', editoptions:{'maxlength':'500', 'class':'form-control', 'rows':'1'}},
		{header:'${text("parent_id")}', name:'parentId.id', editable:true, hidden:true},
		{header:'${text("操作")}', name:'actions', width:80, sortable:false, fixed:true, formatter: function(val, obj, row, act){
			var actions = [];
			if (val == 'new'){
				actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#stMaterialConvertBillDetailDataGrid\').dataGrid(\'delRowData\',\''+obj.rowId+'\')});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
			}else{
				actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#stMaterialConvertBillDetailDataGrid\').dataGrid(\'setRowData\',\''+obj.rowId+'\',null,{display:\'none\'});$(\'#'+obj.rowId+'_status\').val(\''+Global.STATUS_DELETE+'\');});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
			}
			return actions.join('');
		}, editoptions: {defaultValue: 'new'}}
	],

	// 编辑表格参数
	editGrid: true,				// 是否是编辑表格
	editGridInitRowNum: 0,		// 编辑表格的初始化新增行数
	editGridAddRowBtn: $('#stMaterialConvertBillDetailDataGridAddRowBtn'),	// 子表增行按钮
	editGridAddRowInitData: {id: '', status: Global.STATUS_NORMAL},	// 新增行的时候初始化的数据

	// 编辑表格的提交数据参数
	editGridInputFormListName: 'stMaterialConvertBillDetailList', // 提交的数据列表名
	editGridInputFormListAttrs: 'status,id,materialCode,batchNumber,barCode,number,outWarehouse,inWarehouse,convertMaterialCode,converrBarCode,packType,productDate,savedType,passDate,remarks,parentId.id,', // 提交数据列表的属性字段

	shrinkToFit: false,
	// 加载成功后执行事件
	ajaxSuccess: function(data){

	}
});
</script>
<script>
$("#inputForm").validate({
	submitHandler: function(form){
		js.ajaxSubmitForm($(form), function(data){
			if(data.code && 1!=data.code){// 说明是我定义的数据
				js.showMessage(data.data);
				return;
			}
			js.showMessage(data.message);
			refresh(data.data.id);
			// if(data.result == Global.TRUE){
			// 	js.closeCurrentTabPage(function(contentWindow){
			// 		contentWindow.page();
			// 	});
			// }
		}, "json");
    }
});
</script>